package alg.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:yuziji
 * @create: 2023-04-16 15:35
 */
public class SortList {
    public static void main(String[] args) {

        Node head = new Node(2);
        head.next = new Node(3);
        head.next.next = new Node(5);
        head.next.next.next = new Node(1);
        System.out.println(minNode(head));
//        System.out.println(minNode(head).value);
        Node t1 = head;
        while (t1!=null){
            System.out.println(t1.value);
            t1 = t1.next;
        }
        System.out.println("----------------");
        Node node = selectNode(head);
        while(node!=null){
            System.out.println(node.value);
            node = node.next;
        }
    }

    /**
     *
     * 用冒泡排序思路进行链表排序
     * @param head
     */
    private static Node bubbleSortNode(Node head) {
        if (head == null || head.next == null) {
            return head;  // 如果链表为空或只有一个节点，直接返回链表头节点
        }
        boolean isSorted = false; // 标记链表是否已经有序
        while (!isSorted) {
            isSorted = true;
            Node current = head;
            while (current.next != null) {
                if (current.value > current.next.value) {
                    // 交换相邻两个节点的值
                    int temp = current.value;
                    current.value = current.next.value;
                    current.next.value = temp;
                    isSorted = false; // 如果有节点交换，则链表还未有序
                }
                current = current.next;
            }
        }
        return head;
    }

    /**
     * 寻找链表最小值
     * @return
     */
    private static Node minNode(Node head){
        Node min = head;
        while(head!=null){
            if (min.value>head.value){
                min = head;
            }
            head = head.next;
        }
        return min;
    }

    /**
     *
     * @param head
     * @return
     */

    private static Node selectNode(Node head){
        Node temp = head;//当做下标使用
        while(temp.next!=null){
            Node min = minNode(temp);
            if (min.value!=temp.value){
                int num = min.value;
                min.value = temp.value;
                temp.value = num;
            }
            temp = temp.next;
        }
        return head;
    }



}
